package com.batis.exe;

import com.batis.session.SqlSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Objects;

/**
 * @author carter
 * create time    2018-08-17
 */
public class ProxyMapper implements InvocationHandler {

    private SqlSession sqlSession;


    public ProxyMapper(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //找到方法
        Class<?> returnType = method.getReturnType();

        String  ibatisMethod = method.getDeclaringClass().getName() + "." + method.getName();

        Object param = Objects.isNull(args)?null:args[0];

        if (Collection.class.isAssignableFrom(returnType))
        {
            return sqlSession.selectList(ibatisMethod,param);
        }else {
            return sqlSession.selectOne(ibatisMethod,param);
        }

    }
}
